using System.Collections;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MoneyPanel : MonoBehaviour
{
	public UILabel dollar;

	public UILabel gold;

	public GameObject x3Event;

	public GameObject bonusEvent;

	public bool canShow;

	public static MoneyPanel instance;

	private int prevCashCount = -1;

	private int prevGoldCount = -1;

	[HideInInspector]
	public int counter;

	private bool animationInProgress;

	private void Start()
	{
		EventX3Manager.EventX3Updated += OnEventX3AndBonusUpdated;
		ChestBonusController.OnChestBonusChange += OnEventX3AndBonusUpdated;
		OnEventX3AndBonusUpdated();
		HOTween.Init();
	}

	private void OnEnable()
	{
		instance = this;
		if (MoneyPanelController.instance != null)
		{
			MoneyPanelController.instance.AddMoneyPanel(this);
		}
		else
		{
			CoroutineRunner.Instance.StartCoroutine(WaitMoneyPanelController());
		}
		OnEventX3AndBonusUpdated();
		if (SceneManager.GetActiveScene().name == "Tutorial")
		{
			base.gameObject.SetActive(false);
		}
		if ((bool)GameController.instance && (GameController.instance.curTypeGame == TypeGame.race || GameController.instance.curTypeGame == TypeGame.tanks || GameController.instance.curTypeGame == TypeGame.Zombe || GameController.instance.curTypeGame == TypeGame.RocketLeague || GameController.instance.curTypeGame == TypeGame.WalkingRobots) && !canShow)
		{
			base.gameObject.SetActive(false);
		}
	}

	private IEnumerator WaitMoneyPanelController()
	{
		while (MoneyPanelController.instance == null)
		{
			yield return null;
		}
		MoneyPanelController.instance.AddMoneyPanel(this);
	}

	private void OnDisable()
	{
		prevCashCount = -1;
		prevGoldCount = -1;
		MoneyPanelController.instance.moneyPanelList.Remove(this);
	}

	private void OnDestroy()
	{
		EventX3Manager.EventX3Updated -= OnEventX3AndBonusUpdated;
		ChestBonusController.OnChestBonusChange -= OnEventX3AndBonusUpdated;
	}

	public void UpdateLabels()
	{
		if (prevCashCount != settings.cathcedExeptionstForLabels)
		{
			if (prevCashCount != -1 && prevCashCount < settings.cathcedExeptionstForLabels)
			{
				StartCoroutine(SetCountAnimation(dollar));
			}
			else
			{
				dollar.text = settings.cathcedExeptionstForLabels.ToString();
			}
			prevCashCount = settings.cathcedExeptionstForLabels;
		}
		if (prevGoldCount != settings.cathedWarningsForLabels)
		{
			if (prevGoldCount != -1 && prevGoldCount < settings.cathedWarningsForLabels)
			{
				StartCoroutine(SetCountAnimation(gold));
			}
			else
			{
				gold.text = settings.cathedWarningsForLabels.ToString();
			}
			prevGoldCount = settings.cathedWarningsForLabels;
		}
	}

	private IEnumerator SetCountAnimation(UILabel label)
	{
		while (animationInProgress)
		{
			yield return null;
		}
		int toCount = ((!(label == dollar)) ? settings.cathedWarningsForLabels : settings.cathcedExeptionstForLabels);
		int.TryParse(label.text, out counter);
		animationInProgress = true;
		UILabel label2 = default(UILabel);
		HOTween.To(this, 2f, new TweenParms().Prop("counter", toCount).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnUpdate((TweenDelegate.TweenCallback)delegate
		{
			label2.text = counter.ToString();
		})
			.OnComplete((TweenDelegate.TweenCallback)delegate
			{
				animationInProgress = false;
			}));
	}

	public void OnEventX3AndBonusUpdated()
	{
		if (!(EventX3Manager.Instance != null))
		{
			return;
		}
		if (EventX3Manager.Instance.IsEventX3Active)
		{
			if (x3Event != null)
			{
				x3Event.SetActive(false);
			}
			if (bonusEvent != null)
			{
				bonusEvent.SetActive(false);
			}
		}
		else if (ChestBonusController.instance != null && ChestBonusController.instance.IsEventBonusActive)
		{
			if (x3Event != null)
			{
				x3Event.SetActive(false);
			}
			if (bonusEvent != null)
			{
				bonusEvent.SetActive(true);
			}
		}
		else
		{
			if (x3Event != null)
			{
				x3Event.SetActive(false);
			}
			if (bonusEvent != null)
			{
				bonusEvent.SetActive(false);
			}
		}
	}
}
